package com.wc.codeforces.数学.推导.Squaring;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/7/25 17:51
 * @description https://mirror.codeforces.com/contest/1995/problem/C
 */
public class Main {
    /**
     * 思路：真数学
     * log(x^2) = 2 * log(x)
     * 是不是每一个数 * 2就相当于平方了, 但是 2 ^ 1000次方还是有点大
     * 那么我们如法炮制, 再来一次
     * log(2 * log(x)) = log2 + log(log(x))
     * 是不是每一个数 + log2就相当于平方了, 有上述可知道
     * 那我们需要多少次平方呢, 那是不是就是 上取整cnt = ((loglog(a[i - 1]) - loglog(a[i]) / log2))
     * 注意：
     * 前面有 1 是可以去掉的, 但是中间有 1 是完成不了的
     * 还有浮点数做差是需要考虑精度误差
     * 具体可以看代码
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 200010;
    static double eps = 1e-9;
    static double[] a = new double[N];
    static int n;

    public static void main(String[] args) {
        int T = sc.nextInt();
        while (T-- > 0) {
            n = sc.nextInt();
            for (int i = 1; i <= n; i++) a[i] = sc.nextInt();
            int first = 1;
            // 去除前面的 1
            while (a[first] == 1 && first <= n) first++;
            long res = 0;
            // 检查中间是否有 1
            for (int i = first; i <= n; i++) {
                if (a[i] == 1) {
                    res = -1;
                    break;
                }
            }
            if (res == -1) {
                out.println(res);
                continue;
            }
            // 为每一个数 loglog(x)
            for (int i = first - 1; i <= n; i++) a[i] = log2(log2(a[i]));
            for (int i = first; i <= n; i++) {
                double sub = a[i - 1] - a[i];
                if (sub > eps) {
                    // 上取整
                    long cnt = (int) (sub - eps + log2(2) / log2(2));
                    a[i] += cnt * log2(2);
                    res += cnt;
                }
            }
            out.println(res);
        }
        out.flush();
    }

    static double log2(double x) {
        return Math.log(x) / Math.log(2);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
